package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jy.wms.common.PinyinUtils;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.dao.WmsProvinceRankingReportMapper;
import com.jy.wms.pojo.*;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.collections.map.LinkedMap;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

import static com.jy.wms.common.base.BaseController.getJSONObject;

/**
 * @ClassName: WmsProvinceRankingReportService.java
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author lqc
 * @Date Thu Mar 22 18:05:53 CST 2018
 */

@Service
public class WmsProvinceRankingReportService extends BaseService<WmsProvinceRankingReportMapper, WmsProvinceRankingReport> {


    /**
     * 直销省份排名报表查询（根据时间查询）

     * @return JSONObject
     */

    public JSONObject baseselectbyprimarykey(Map<String, Object> mapp, String hzid, String ckid) {
        //将数据添加到map集合中
        HashMap<Object, Object> map = new HashMap<Object, Object>();
        JSONObject allJson = new JSONObject();
        try {
            String date = (String) mapp.get("date");
            int page = (int) mapp.get("page");
            int rows = (int) mapp.get("rows");
            //查询直销省份排名报表
            PageHelper.startPage(page, rows);
            List<WmsProvinceRankingReport> Sales = dao.baseselectbyprimarykey(date, hzid, ckid);

            int i = 0;//记录排名
            int sddl = 0;//记录订单量
            int zddl = 0;
            double Ddslbfb = 0;
            int zxse = 0;
            double zddbfb = 0;
            int a = 1;
            for (WmsProvinceRankingReport user : Sales) {
                //订单量排序
                if (sddl == 0) {
                    sddl = Integer.parseInt(user.getDdl());
                    i++;
                } else {
                    if (Integer.parseInt(user.getDdl()) == sddl) {
                    } else {
                        sddl = Integer.parseInt(user.getDdl());
                        i++;
                    }
                }
                user.setDdlpm(i);
                //总计
                if (user.getDdl() != null) {
                    //统计订单量总计
                    int ddL = Integer.parseInt(user.getDdl());
                    zddl += ddL;
                }
                //统计总订单百分比
                String str = user.getDdlbfb().substring(0, user.getDdlbfb().length() - 1);
                double DdslbfB = Double.parseDouble(str);
                Ddslbfb += DdslbfB;

                //统计总销售额
                if (user.getXse() != null) {
                    int zxsE = Integer.parseInt(user.getXse());
                    zxse += zxsE;
                }
                if (user.getXsezb() != null) {
                    //统计总销售额百分比
                    String jq = user.getDdlbfb().substring(0, user.getDdlbfb().length() - 1);
                    double zddbfB = Double.parseDouble(jq);
                    zddbfb += zddbfB;
                }
            }
            allJson.put("contentList", Sales);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return allJson;
    }

    /**
     * 各省运费统计（按产品大类）
     *
     * @param
     * @return list
     */
    public JSONObject freightStatisticsInVariousProvinces(Map<String, Object> mapp, String hzid, String ckid) {
        JSONObject JSON = new JSONObject();
        PinyinUtils pinyin = new PinyinUtils();
        Map<String, Object> map = null;
        String state = (String) mapp.get("state");
        int page = (int) mapp.get("page");
        int rows = (int) mapp.get("rows");
        PageHelper.startPage(page, rows);
        Page<Map<String, Object>> Thenumber = new Page<>();
        List list;
        if (state != null && state != "") {
            //单独查询出省
            list = dao.freightStatisticsInVarious(state, hzid, ckid);

            //取出种类
            List<WmsHead> Frei = dao.WmsFreightHead(state, hzid, ckid);
            int d = 1;//累加
            Map<String, Object> zhh = new LinkedHashMap<>();
            String sheng = null;
            int zzl = 0;//该省的所有商品的总量
            double yf = 0.0;//单个运费
            double zyf = 0.0;//每个省的运费汇总
            double ZZ = 0.0;//求百分比
            String dgzl = null;//记录每个种类

            List<WmsFreightStatisticsInVariousProvinces> Sales = null;
            for (int str = 0; str < list.size(); str++) {
                if (list.get(str)!=null && list.get(str)!=""){
                    map = new LinkedHashMap<>();
                    for (WmsHead use : Frei) {
                        String type = use.getMc();
                        Sales = dao.freightStatisticsInVariousProvinces(list.get(str) + "", state, type, hzid, ckid);
                        if (Sales.isEmpty()) {
                            map.put("Province", list.get(str));
                            String ty = pinyin.ToPinyin(type);
                            map.put(ty, 0.0);
                        }
                        for (WmsFreightStatisticsInVariousProvinces user : Sales) {
                            dgzl = user.getMc();
                            //判断是否是同一个种类
                            if (user.getMc().equals(dgzl)) {
                                //取出每个种类的重量信息
                                double Z = Double.parseDouble(user.getMzzl());
                                zzl += Z;
                            } else if (user.getMc() != dgzl) {
                                dgzl = "";
                                zzl = 0;
                                //z=0;
                                //取出每个种类的重量信息
                                double Z = Double.parseDouble(user.getMzzl());
                                zzl += Z;

                            }

                            if (zzl == 0 || user.getMzzl().equals(0.00)) {
                                //返回0
                                yf = 0;
                                String ty = pinyin.ToPinyin(user.getMc());
                                map.put(ty, yf + "");
                            } else {
                                //类型进行转换
                                double YDSL = Double.parseDouble(user.getMzzl());
                                //进行百分比运算
                                ZZ = YDSL / zzl;
                                double ZYF = Double.parseDouble(user.getZyf());
                                yf = ZYF * ZZ;
                                BigDecimal b = new BigDecimal(yf);
                                yf = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                                String ty = pinyin.ToPinyin(user.getMc());
                                map.put(ty, yf + "");
                            }

                            if (sheng == null) {
                                sheng = user.getSheng();
                                map.put("Province", list.get(str));
                                //每个省的运费汇总
                                zyf += yf;
                                BigDecimal b = new BigDecimal(zyf);
                                zyf = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                                map.put("FreightCollection", zyf + "");
                            } else {
                                if (sheng.equals(user.getSheng())) {
                                    //每个省的运费汇总
                                    zyf += yf;

                                    BigDecimal b = new BigDecimal(zyf);
                                    zyf = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                                    map.put("FreightCollection", zyf + "");
                                } else if (sheng != user.getSheng()) {
                                    zyf = 0.0;
                                    sheng = user.getSheng();
                                    map.put("Province", list.get(str));
                                    //每个省的运费汇总
                                    zyf += yf;
                                    BigDecimal b = new BigDecimal(zyf);
                                    zyf = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                                    map.put("FreightCollection", zyf + "");
                                }
                            }
                        }
                    }
                    Thenumber.add(map);
                }
            }
            JSON.put("contentList", Thenumber);
            JSONArray array = JSONArray.fromObject(Thenumber);
            JSON = SetStatus.setSearchType(array.toString(), (int) Thenumber.getTotal());
        }
        return JSON;
    }


    /**
     * 各省发货支数统计（按产品大类）
     *
     * @param
     * @return list
     */
    public JSONObject thenumberofshipmentsineachprovince(Map<String, Object> mapp, String hzid, String ckid) {

        PinyinUtils pinyin = new PinyinUtils();
        JSONObject JSON = new JSONObject();
        Map<String, Object> map;
        //添加总计
        String state = (String) mapp.get("state");
        int page = (int) mapp.get("page");
        int rows = (int) mapp.get("rows");
        PageHelper.startPage(page, rows);
        Page<Map<String, Object>> Thenumber = new Page<>();

        if (state != null && state != "") {
            //取出所有省份
            List list = dao.freightStatistics(state, hzid, ckid);
            //取出所有种类
            List<WmsHead> su = dao.supportnumberhead(state, hzid, ckid);

            String sheng = null;//判断省份
            int mc = 0;//定义总计
            int zzl = 0;//该省的支数汇总
            List<WmsShipmentsInEachProvince> Sales = null;
            for (int str = 0; str < list.size(); str++) {
                if (list.get(str)!=null && list.get(str)!=""){
                    map = new LinkedHashMap<>();
                    for (WmsHead use : su) {
                        String type = use.getMc();
                        Sales = dao.thenumberofshipmentsineachprovince(list.get(str) + "", state, type, hzid, ckid);
                        if (Sales.isEmpty()) {
                            map.put("Province", list.get(str));
                            String ty = pinyin.ToPinyin(type);
                            map.put(ty, 0);
                        }
                        for (WmsShipmentsInEachProvince user : Sales) {
                            String ty = pinyin.ToPinyin(user.getMc());
                            map.put(ty, user.getZs());
                            if (sheng == null) {
                                sheng = user.getSheng();
                                map.put("Province", list.get(str));
                                int zs = Integer.parseInt(user.getZs());
                                map.put("SupportNumberSummary", zzl += zs);
                            } else {
                                if (sheng.equals(user.getSheng())) {
                                    int zs = Integer.parseInt(user.getZs());
                                    map.put("SupportNumberSummary", zzl += zs);
                                } else if (sheng != user.getSheng()) {
                                    zzl = 0;
                                    sheng = user.getSheng();
                                    map.put("Province", list.get(str));

                                    int zs = Integer.parseInt(user.getZs());
                                    map.put("SupportNumberSummary", zzl += zs);
                                }
                            }
                        }
                    }
                    Thenumber.add(map);
                }
            }
            JSON.put("contentList", Thenumber);
            JSONArray array = JSONArray.fromObject(Thenumber);
            JSON = SetStatus.setSearchType(array.toString(), (int) Thenumber.getTotal());
        }
        return JSON;
    }

    /**
     * 各省销售额统计（按产品大类）
     *
     * @param
     * @return list
     */
    public JSONObject statisticsofsalesineachprovince(Map<String, Object> mapp, String hzid, String ckid) {

        JSONObject JSON = new JSONObject();
        PinyinUtils pinyin = new PinyinUtils();
        Map<String, Object> map;
        String state = (String) mapp.get("state");
        int page = (int) mapp.get("page");
        int rows = (int) mapp.get("rows");
        PageHelper.startPage(page, rows);
        Page<Map<String, Object>> Thenumber = new Page<>();

        if (state != null && state != "") {
            //单独查询出省
            List list = dao.fsalesineachprovince(state, hzid, ckid);
            //单独查询出种类
            List<WmsHead> Salesvolume = dao.Saleshead(state, hzid, ckid);

            String mc = null;//判断规格
            String sheng = null;//判断省份
            double xse = 0.0;
            List<WmsStatisticsoFsalesIneachProvince> Sales = null;
            //将省份进行遍历
            for (int str = 0; str < list.size(); str++) {
                if (list.get(str)!=null && list.get(str)!=""){
                    map = new HashMap<>();
                    for (WmsHead use : Salesvolume) {
                        String type = use.getMc();
                        //根据指定省份查询出销售额
                        Sales = dao.statisticsofsalesineachprovince(list.get(str) + "", state, type, hzid, ckid);
                        if (Sales.isEmpty()) {
                            String ty = pinyin.ToPinyin(type);
                            map.put(ty, 0);
                            map.put("Province", list.get(str));
                        }
                        for (WmsStatisticsoFsalesIneachProvince user : Sales) {
                            //map.put("Categoryname" + d++, user.getMc());
                            double Z = Double.parseDouble(user.getHpje());
                            BigDecimal b = new BigDecimal(Z);
                            Z = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                            String ty = pinyin.ToPinyin(user.getMc());
                            map.put(ty, Z);


                            if (sheng == null) {
                                sheng = user.getSheng();
                                map.put("Province", list.get(str));
                                double je = Double.parseDouble(user.getHpje());
                                xse += je;
                                BigDecimal B = new BigDecimal(xse);
                                xse = B.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                                map.put("SalesVolumeSummary", xse);
                            } else {
                                if (sheng.equals(user.getSheng())) {
                                    double je = Double.parseDouble(user.getHpje());
                                    xse += je;
                                    BigDecimal B = new BigDecimal(xse);
                                    xse = B.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                                    map.put("SalesVolumeSummary", xse);
                                } else if (sheng != user.getSheng()) {
                                    xse = 0.0;
                                    sheng = user.getSheng();
                                    map.put("Province", list.get(str));

                                    double je = Double.parseDouble(user.getHpje());
                                    xse += je;
                                    BigDecimal B = new BigDecimal(xse);
                                    xse = B.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                                    map.put("SalesVolumeSummary", xse);
                                }
                            }
                        }
                    }
                    Thenumber.add(map);
                }
            }
            JSON.put("contentList", Thenumber);
            JSONArray array = JSONArray.fromObject(Thenumber);
            JSON = SetStatus.setSearchType(array.toString(), (int) Thenumber.getTotal());
        }
        return JSON;
    }

    /**
     * 各省订单量统计（按产品大类）
     *
     * @param
     * @return list
     */
    public JSONObject statisticsoforderquantityineachprovince(Map<String, Object> mapp, String hzid, String ckid) {
        JSONObject JSON = new JSONObject();
        PinyinUtils pinyin = new PinyinUtils();
        Map<String, Object> map;
        String state = (String) mapp.get("state");
        int page = (int) mapp.get("page");
        int rows = (int) mapp.get("rows");
        PageHelper.startPage(page, rows);
        Page<Map<String, Object>> Thenumber = new Page<>();

        if (state != null && state != "") {
            //单独查询出省
            List list = dao.statisticsoforderquantity(state, hzid, ckid);
            //查出所有种类
            List<WmsHead> Order = dao.Orderquantityheader(state, hzid, ckid);
            String sheng = "";//判断省份
            int zzl = 0;//该省的所有商品的总销售额
            List<WmsShipmentsInEach> Sales = null;
            try {
                for (int str = 0; str < list.size(); str++) {
                    map = new HashMap<>();
                    for (WmsHead use : Order) {
                        String type = use.getMc();
                        Sales = dao.statisticsoforderquantityineachprovince(list.get(str) + "", state, type, hzid, ckid);
                        if (Sales.isEmpty()) {
                            map.put("Province", list.get(str));
                            String ty = pinyin.ToPinyin(type);
                            map.put(ty, 0);
                        }
                        for (WmsShipmentsInEach user : Sales) {
                            String ty = pinyin.ToPinyin(user.getMc());
                            map.put(ty, user.getFybh());

                            if (sheng == "") {
                                sheng = user.getSheng();
                                map.put("Province", list.get(str));
                                int zs = Integer.parseInt(user.getFybh());
                                map.put("OrderQuantitySummary", zzl += zs);
                            } else {
                                if (sheng.equals(user.getSheng())) {
                                    int zs = Integer.parseInt(user.getFybh());
                                    map.put("OrderQuantitySummary", zzl += zs);
                                } else if (sheng != user.getSheng()) {
                                    String ss = list.get(str).toString();
                                    if (ss.equals("")) {
                                        int zs = Integer.parseInt(user.getFybh());
                                        map.put("OrderQuantitySummary", zzl += zs);
                                    } else {
                                        zzl = 0;
                                        sheng = user.getSheng();
                                        map.put("Province", list.get(str));
                                        int zs = Integer.parseInt(user.getFybh());
                                        map.put("OrderQuantitySummary", zzl += zs);
                                    }
                                }
                            }
                        }
                    }
                    Thenumber.add(map);
                }
                JSON.put("contentList", Thenumber);
                JSONArray array = JSONArray.fromObject(Thenumber);
                JSON = SetStatus.setSearchType(array.toString(), (int) Thenumber.getTotal());
            } catch (Exception e) {
                e.getMessage();
            }
        }
        return JSON;
    }



}
